﻿//Srinivasan

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace Tzync
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ThreadException +=
               new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ProfileUIForm());
        }

        public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            DialogResult result = DialogResult.Abort;
            String exePath = Directory.GetCurrentDirectory();

            Exception ex = (Exception)e.Exception;            

            if (!(ex is System.IO.DirectoryNotFoundException || ex is System.IO.FileNotFoundException))
            {
                result = MessageBox.Show("An unknown error has occurred.\n\nTzync will now exit.\n\n.Please contact V-Sync with the details of the error.\n\n" + e.Exception.Message,
                 "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
                return;
            }            

            HDDplugin:
            result = MessageBox.Show("Your hard disk is unplugged!\n\nPlug it in and click OK to restart Tzync, or click Cancel to quit the application.\n\nUpon restart, Tzync will safely recover your files and you can sync again",
                "Hard disk unplugged!", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);

            if (result == DialogResult.OK)
            {
                DriveInfo drv = new DriveInfo(exePath);
                if (drv.IsReady)
                    Application.Restart();
                else
                    goto HDDplugin;
            }
            else
                Application.Exit();
        }
    }
}
